<?php

/*
 * $Id: table.php 12 2010-11-28 20:02:53Z LEON $
 * $Author: LEON $
 * $LastChangedDate: 2010-11-28 20:02:53 +0000 (Sun, 28 Nov 2010) $
 * $Revision: 12 $
 */

namespace cmFly\system\data_model\orm;

//TODO: Реализовать сохранение таблицы и прочие радости...
abstract class table extends \cmFly\data_model\base {

    protected static $indexes;
    private static $all_indexes;
    protected static $fields;
    private static $all_fields;
    private static $table_name;
    protected static $primary;
    protected static $relations=Array();
    const TYPE='type';
    const TYPE_INT=1;
    const TYPE_CHAR=2;
    const TYPE_FLOAT=3;
    const PRIMARY='primary';
    const LAZY='lazy';
    const RELATION_FIELD='field';
    const RELATION_CLASS='class';
    const RELATION_CLASS_RELATION='relation';
    const RELATION_TYPE='type';
    const RELATION_ONE_TO_ONE=1;
    const RELATION_ONE_TO_MANY=2;
    const RELATION_MANY_TO_MANY=3;

    const RELATIONS_ALL=3;
    const RELATIONS_LAZY=1;
    const RELATIONS_NOLAZY=2;

    public function __construct() {
	parent::__construct();
    }

    public static function create() {
	
    }
    
    public static function update(table $new) {
	return \get_parent_class(\get_called_class());
    }

    public static function get_name() {
	$called_class = \get_called_class();
	if (!isset(self::$table_name[$called_class])) {
	    self::$table_name[$called_class] = basename(dirname($called_class));
	}
	return self::$table_name[$called_class];
    }

    public static function get_fields() {
	$called_class = \get_called_class();
	if (!isset(self::$all_fields[$called_class])) {
	    $class = \get_parent_class(\get_called_class());
	    $all_fields = Array();
	    if (\get_called_class() != __CLASS__) {
		$all_fields = $class::get_fields();
	    }
	    if (isset(static::$fields)) {
		$all_fields += static::$fields;
	    }
	    self::$all_fields[$called_class] = $all_fields;
	}
	return self::$all_fields[$called_class];
    }

    public static function field_exists($field) {
	$all_fields=static::get_fields();
	return isset($all_fields[$field]);
    }

    public static function get_relations($relation_collection=self::RELATIONS_ALL) {
	return static::$relations;
    }

    public static function relation_class($relation_name) {
	$relations=static::get_relations();
	$class_name=$relations[$relation_name][self::RELATION_CLASS];
	$type=($relations[$relation_name][self::RELATION_TYPE]==self::RELATION_ONE_TO_ONE ? 'model' : 'collection');
	if(\substr($class_name,0,1)=='\\') {
	    return $class_name.'\\'.$type;
	}
	return '\\'.\dirname(\dirname(\get_called_class())).'\\'.$class_name.'\\'.$type;
    }
    
    public static function get_indexes() {
	$called_class = \get_called_class();
	if (!isset(self::$all_indexes[$called_class])) {
	    $class = \get_parent_class(\get_called_class());
	    $all_indexes = Array();
	    if (\get_called_class() != __CLASS__) {
		$all_indexes = $class::get_indexes();
	    }
	    if (isset(static::$indexes)) {
		$all_indexes += static::$indexes;
	    }
	    self::$all_indexes[$called_class] = $all_indexes;
	}
	return self::$all_indexes[$called_class];
    }
    public static function get_primary() {
	if(!static::$primary) {
	    throw new \cmFly\data_model\exception('Primary key is undefined!');
	}
	return static::$primary;
    }
}